package com.juyu.miliao.api.service.whitepacket;

import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.CommentReplyInfo;
import com.juyu.miliao.api.domain.WhitePacketCommentReplyInfo;
import com.juyu.miliao.api.domain.WhitePacketDynamicCommentInfo;
import com.juyu.miliao.api.domain.vo.WhitePacketDynamicCommentInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuanqicheng
 * @create 2019-14-14:44
 */
@Service
public class BasicWhitePacketService {

    @Autowired
    private UserInfoCache userInfoCache;

    /**
     * 设置白包动态评论基本信息
     * @param dynamicCommentInfoVo
     * @param commentInfo
     */
    public void setDynamicCommentInfoVo(WhitePacketDynamicCommentInfoVo dynamicCommentInfoVo,
                                        WhitePacketDynamicCommentInfo commentInfo) {

        int nowYear= LocalDateTime.now().getYear();

        int year = commentInfo.getCommentTime().getYear();

        String showTime;

        if(nowYear==year){
            LocalDateTime time = commentInfo.getCommentTime();
            showTime = time.getMonthValue()+"-"+time.format(DateTimeFormatter.ofPattern("dd HH:mm"));
        }else{
            showTime = commentInfo.getCommentTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
        }
        dynamicCommentInfoVo.setCommentId(commentInfo.getId());

        BeanUtils.copyProperties(commentInfo, dynamicCommentInfoVo);

        UserCacheVo commentUserInfo = userInfoCache.getUserCacheInfo(commentInfo.getFromUserId());

        dynamicCommentInfoVo.setFromUserNickName(commentUserInfo.getNickName())
                .setFromUserIcon(commentUserInfo.getIcon())
                .setFromUserAge(commentUserInfo.getAge())
                .setFromUserIsHost(commentUserInfo.getIsHost())
                .setFromUserIsVip(commentUserInfo.getIsVip())
                .setFromUserMGrade(commentUserInfo.getMGrade())
                .setFromUserVGrade(commentUserInfo.getVGrade())
                .setShowTime(showTime)
                .setFromUserSex(commentUserInfo.getSex());

    }

    public Map<String, Object> setCommentReplyInfo(int size, WhitePacketCommentReplyInfo commentReplyInfo) {

        UserCacheVo replyFromUserInfo = userInfoCache.getUserCacheInfo(commentReplyInfo.getReplyFromUserId());

        String replyFromUserNickName = replyFromUserInfo.getNickName();

        UserCacheVo replyToUserInfo = userInfoCache.getUserCacheInfo(commentReplyInfo.getReplyToUserId());

        String replyToUserNickName = replyToUserInfo.getNickName();

        Map<String,Object> map = new HashMap<>(2);

        map.put("size",size);

        map.put("replyFromUserNickName",replyFromUserNickName);

        map.put("replyToUserNickName",replyToUserNickName);

        map.put("replyContent",commentReplyInfo.getReplyContent());


        return map;
    }
}
